<template>
  <div class="overview-container">
    <!-- <p class="overview-title">服务总览</p> -->
    <!-- 服务总览网格 -->
    <div class="grid-container">
      <div class="grid-title">服务总览</div>
      <div class="stats-grid">
        <!-- 服务任务总数 - 跨两行 -->
        <div class="grid-item service-task span-two-rows">
          <i class="icon-picture el-icon-s-data" />
          <p class="card-number">{{ summaryCards.serviceTaskNum }}</p>
          <p class="card-description">服务任务总数</p>
        </div>
        
        <!-- 训练任务总数 - 跨两行 -->
        <div class="grid-item training-task span-two-rows">
          <i class="icon-picture el-icon-s-grid" />
          <p class="card-number">{{ summaryCards.trainingTaskNum }}</p>
          <p class="card-description">训练任务总数</p>
        </div>
        
        <!-- 其他8个项目 -->
        <div 
          v-for="(item, index) in listData1" 
          :key="index" 
          class="grid-item stat-item"
          :class="getItemClass(item)"
        >
          <p class="stat-number">{{ item.text1 }}</p>
          <p class="card-description">{{ item.text }}</p>
        </div>
      </div>
    </div>
    
    <div class="details-section">
      <el-card class="details-card">
        <div class="details-header">
          <p class="details-title">服务明细</p>
          <div class="details-actions">
            <el-input v-model="queryForm.user" placeholder="请输入操作人" class="input-operator" />
            <el-select v-model="queryForm.serviceStatus" placeholder="服务状态" class="select-status">
              <el-option
                v-for="item in serviceStatusOptions"
                :key="item.key"
                :label="item.value"
                :value="item.key">
              </el-option>
            </el-select>
            <el-button type="primary" class="button-query" @click="handleQuery">查询</el-button>
            <el-button class="button-reset" @click="handleReset">重置</el-button>
          </div>
        </div>
        <el-table :data="dataSource" class="table-container" :row-class-name="tableRowClassName" border>
          <el-table-column v-for="(col, index) in columns" :key="col.key" :prop="col.dataIndex" :label="col.title" :width="col.width" :align="index === 0 ? 'left' : 'center'">
            <template v-slot:default="scope">
              <template v-if="col.dataIndex === 'description'">
                <span class="status-dot" :class="getStatusClass(scope.row.description)"></span>
                <span class="status-text">{{ getDescriptionText(scope.row.description) }}</span>
              </template>
              <template v-else-if="col.dataIndex === 'serviceTime'">
                <span>{{ scope.row.serviceTime }}</span>
              </template>
              <template v-else>
                <span>{{ scope.row[col.dataIndex] }}</span>
              </template>
            </template>
          </el-table-column>
        </el-table>
        <div class="pagination-container">
          <el-pagination 
            background
            :current-page="currentPage" 
            :page-size="pageSize" 
            :total="total" 
            :page-sizes="[10, 20, 30, 40, 50]" 
            layout="prev, pager, next, sizes, jumper,total"
            @current-change="handleCurrentChange"
            @size-change="handleSizeChange"
          />
        </div>
      </el-card>
    </div>
  </div></template><script>
import { getServiceCardList, getServiceList, getServiceStatusOptions } from '@/api/ServiceDashboardAPI'

export default {
  data() {
    return {
      // 服务总览卡片数据
      summaryCards: {
        serviceTaskNum: 0, // 服务任务总数
        trainingTaskNum: 0, // 训练任务总数
        finishedTaskNum: 0, // 已完成训练
        reasoningTaskNum: 0, // 推理任务总数
        training: 0, // 训练中
        running: 0, // 运行中
        queue: 0, // 排队中
        deleted: 0, // 已停止
        trainingFailed: 0, // 训练失败
        runningFailed: 0 // 运行失败
      },
      // 统计数据列表（用于渲染网格）
      listData1: [
        { text: "已完成训练", text1: "0", type: "finishedTaskNum" },
        { text: "训练中", text1: "0", type: "training" },
        { text: "排队中", text1: "0", type: "queue" },
        { text: "训练失败", text1: "0", type: "trainingFailed" },
        { text: "推理任务总数", text1: "0", type: "reasoningTaskNum" },
        { text: "运行中", text1: "0", type: "running" },
        { text: "已停止", text1: "0", type: "deleted" },
        { text: "运行失败", text1: "0", type: "runningFailed" },
      ],
      columns: [
        { title: "服务名称", dataIndex: "serviceName", key: "serviceName", width: 200 },
        { title: "服务类型", dataIndex: "serviceType", key: "serviceType", width: 150 },
        { title: "描述", dataIndex: "description", key: "description" },
        { title: "服务启停时间", dataIndex: "serviceOnOffTime", key: "serviceOnOffTime", width: 300 },
        { title: "操作人", dataIndex: "user", key: "user", width: 150 },
        { title: "占用资源数（GPU）", dataIndex: "usedGPU", key: "usedGPU", width: 150 },
        { title: "占用资源数（CPU）", dataIndex: "usedCPU", key: "usedCPU", width: 150 },
        { title: "占用资源数（内存）", dataIndex: "usedMemory", key: "usedMemory", width: 150 },
      ],
      dataSource: [],
      currentPage: 1,
      pageSize: 10,
      total: 0,
      serviceStatusOptions: [], // 服务状态下拉框选项
      queryForm: {
        user: '', // 操作人查询条件
        serviceStatus: '' // 服务状态查询条件
      }
    };
  },
  created() {
    this.initData();
  },
  methods: {
    async initData() {
      // 并行请求所有初始化数据
      await Promise.all([
        this.fetchServiceCardList(),
        this.fetchServiceStatusOptions(),
        this.fetchServiceList()
      ]);
    },
    
    // 获取服务总览卡片数据
    async fetchServiceCardList() {
      try {
        const response = await getServiceCardList();
        console.log('getServiceCardList response:', response);
        
        // 将返回值赋值给summaryCards中对应的字段
        this.summaryCards = {
          serviceTaskNum: response.serviceTaskNum || 0,
          trainingTaskNum: response.trainingTaskNum || 0,
          finishedTaskNum: response.finishedTaskNum || 0,
          reasoningTaskNum: response.reasoningTaskNum || 0,
          training: response.training || '0',
          running: response.running || '0',
          queue: response.queue || 0,
          deleted: response.deleted || 0,
          trainingFailed: response.trainingFailed || 0,
          runningFailed: response.runningFailed || 0
        };
        
        // 更新listData1中的数值
        this.listData1.forEach(item => {
          item.text1 = this.summaryCards[item.type].toString();
        });
        console.log('Updated summaryCards:', this.summaryCards);
        console.log('Updated listData1:', this.listData1);
      } catch (error) {
        console.error('获取服务总览卡片数据失败:', error);
      }
    },
    
    // 获取服务状态下拉框选项
    async fetchServiceStatusOptions() {
      try {
        const response = await getServiceStatusOptions();
        console.log('getServiceStatusOptions response:', response);
        this.serviceStatusOptions = response || [];
      } catch (error) {
        console.error('获取服务状态下拉框选项失败:', error);
      }
    },
    
    // 获取服务列表数据
    async fetchServiceList() {
      try {
        const params = {
          user: this.queryForm.user,
          serviceStatus: this.queryForm.serviceStatus,
          pageNo: this.currentPage,
          pageSize: this.pageSize
        };
        
        const response = await getServiceList(params);
        console.log('getServiceList response:', response);
        this.dataSource = response.list || [];
        this.total = response.total || response.records || 0;
      } catch (error) {
        console.error('获取服务列表数据失败:', error);
      }
    },
    
    tableRowClassName({ row, rowIndex }) {
      if (rowIndex % 2 === 0) {
        return 'even-row';
      }
      return 'odd-row';
    },
    
    getStatusClass(status) {
      switch (status) {
        case 'running':
          return 'status-running';
        case 'stopped':
          return 'status-stopped';
        case 'deleted':
          return 'status-deleted';
        case 'fault':
          return 'status-fault';
        default:
          return '';
      }
    },
    
    getDescriptionText(status) {
      switch (status) {
        case 'running':
          return '运行中';
        case 'stopped':
          return '已停止';
        case 'deleted':
          return '已删除';
        case 'fault':
          return '故障';
        default:
          return status;
      }
    },
    
    handleCurrentChange(val) {
      this.currentPage = val;
      this.fetchServiceList();
    },
    
    handleSizeChange(val) {
      this.pageSize = val;
      this.currentPage = 1;
      this.fetchServiceList();
    },
    
    getItemClass(item) {
      return 'item-' + item.type;
    },
    
    // 查询按钮点击事件
    handleQuery() {
      this.currentPage = 1;
      this.fetchServiceList();
    },
    
    // 重置按钮点击事件
    handleReset() {
      this.queryForm.user = '';
      this.queryForm.serviceStatus = '';
      this.currentPage = 1;
      this.fetchServiceList();
    }
  }
};

</script>

<style lang="scss" scoped>
.overview-container {
  background-color: #f7f6f5;
  padding: 0 16px;
  height: 100%;
  overflow: auto;
  box-sizing: border-box;
  

  .overview-title {
    font-size: 18px;
    font-weight: bold;
    margin-bottom: 16px;
    color: #000000;
  }

  .grid-container {
    background-color: #FFFFFF;
    border-radius: 4px;
    padding: 16px;
    margin-bottom: 16px;
    
    .grid-title {
      font-size: 18px;
      font-weight: bold;
      color: #000000;
      margin-bottom: 16px;
    }
    
    .stats-grid {
      display: grid;
      grid-template-columns: repeat(6, 1fr);
      grid-template-rows: repeat(2, 120px);
      gap: 16px;
      
      .grid-item {
        display: flex;
        flex-direction: column;
        justify-content: center;
        align-items: center;
        border: 1px solid #EEEEEE;
        border-radius: 4px;
        padding: 8px;
        
        .icon-picture  {
          font-size: 32px;
          color: #2196F3;
          padding: 8px;
          border-radius: 50%;
          background-color: #E3F2FD;
          margin-bottom: 8px;
        }
        
        .card-number {
          font-size: 24px;
          font-weight: bold;
          color: #000000;
          margin-bottom: 8px;
        }
        
        .card-description {
          color: #000000;
          text-align: center;
        }
        
        .stat-number {
          font-size: 24px;
          font-weight: bold;
          margin-bottom: 8px;
        }
      }
      
      .span-two-rows {
        grid-row: span 2;
      }
      
      .service-task {
        .card-icon, .card-number, .card-description {
          display: block;
        }
      }
      
      .training-task {
        .card-icon, .card-number, .card-description {
          display: block;
        }
      }
      
      .stat-item {
        .card-icon {
          display: none;
        }
      }
      
      // 颜色类 - 根据图片要求调整颜色
      .item-finishedTaskNum .stat-number {
        color: #00C853; // 绿色 - 已完成训练
      }
      
      .item-training .stat-number,
      .item-running .stat-number {
        color: #2196F3; // 蓝色 - 训练中、运行中
      }
      
      .item-queue .stat-number,
      .item-deleted .stat-number {
        color: #FF9800; // 橙色 - 排队中、已停止
      }
      
      .item-trainingFailed .stat-number,
      .item-runningFailed .stat-number {
        color: #F44336; // 红色 - 训练失败、运行失败
      }
      
      .item-reasoningTaskNum .stat-number {
        color: #000000; // 黑色 - 推理任务总数
      }
    }
  }
}

.details-section {
  background-color: #FFFFFF;
  padding: 0 16px;
  border-top: 1px solid #EEEEEE;
  
  .details-card {
    border: none;
    box-shadow: none;
    
    .details-header {
      display: flex;
      justify-content: space-between;
      align-items: center;
      margin-bottom: 16px;

      .details-title {
        font-size: 18px;
        font-weight: bold;
        color: #000000;
      }

      .details-actions {
        display: flex;
        align-items: center;

        .input-operator {
          width: 200px;
          height: 36px;
          margin-right: 16px;
        }

        .select-status {
          width: 150px;
          height: 36px;
          margin-right: 16px;
        }

        .button-query {
          background-color: #2196F3;
          border-color: #2196F3;
          color: #FFFFFF;
          width: 80px;
          height: 36px;
          border-radius: 4px;
          margin-right: 8px;
        }

        .button-reset {
          background-color: #999999;
          border-color: #999999;
          color: #FFFFFF;
          width: 80px;
          height: 36px;
          border-radius: 4px;
        }
      }
    }
  }

  .table-container {
    margin-top: 16px;
    
    ::v-deep &.el-table {
      border: 1px solid #EEEEEE;
      
      .el-table__header-wrapper {
        th {
          background-color: #F5F5F5;
          color: #000000;
          font-weight: bold;
        }
      }
      
      // .even-row {
      //   background-color: #FFFFFF;
      //   td {
      //     color: #000000;
      //   }
      // }
      
      // .odd-row {
      //   background-color: #F9F9F9;
      //   td {
      //     color: #000000;
      //   }
      // }
      
      .status-dot {
        display: inline-block !important;
        width: 8px !important;
        height: 8px !important;
        border-radius: 50% !important;
        margin-right: 5px !important;
        
        &.status-running {
          background-color: #00C853 !important;
        }
        
        &.status-stopped {
          background-color: #F44336 !important;
        }
        
        &.status-deleted {
          background-color: #9E9E9E !important;
        }
        
        &.status-fault {
          background-color: #FF9800 !important;
        }
      }
      
      .status-text {
        color: #000000;
      }
    }
  }

  .pagination-container {
    display: flex;
    justify-content: center;
    align-items: center;
    margin-top: 16px;
    
    ::v-deep .el-pagination {
      .btn-prev, .btn-next, .el-pager li {
        background-color: #FFFFFF;
        color: #000000;
        // border: 1px solid #CCCCCC;
        border-radius: 4px;
        margin: 0 2px;
        text-align: center;
        
        &.active {
          background-color: #2196F3;
          color: #FFFFFF;
          border-color: #2196F3;
        }
        
        &:not(.disabled):hover {
          color: #2196F3;
        }
      }
      
      .el-pagination__sizes {
        color: #000000;
        margin: 0 16px;
        
        .el-input .el-input__inner {
          // border: 1px solid #CCCCCC;
          background-color: #FFFFFF;
          border-radius: 4px;
          color: #000000;
        }
      }
      
      .el-pagination__jump {
        color: #000000;
        
        .el-pagination__editor {
          // border: 1px solid #CCCCCC;
          background-color: #FFFFFF;
          width: 50px;
          border-radius: 4px;
          margin: 0 8px;
          color: #000000;
        }
      }
      
      .el-pagination__total {
        color: #000000;
      }
    }
  }
}</style>